package com.example.myrecyclerview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.myrecyclerview.bean.Book;

public class EditBookActivity extends AppCompatActivity {

    private EditText mEtBook;
    private Button mBtnCommit,mBtnCancel;
    private String bookName;
    int startType,itemPosition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_book);
        initData();
        initView();
    }


    private void initData() {
        Intent intent = getIntent();
        bookName = intent.getStringExtra("bookName");
        startType = intent.getIntExtra("startType",0);
        itemPosition = intent.getIntExtra("itemPosition",0);
    }

    private void initView() {
        mEtBook = findViewById(R.id.et_book_name);
//        Log.d("<Test>",startType);
        if(startType==MyConst.UPDATE_BOOK)
            mEtBook.setText(bookName);
        mBtnCommit = findViewById(R.id.btn_commit);
        mBtnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bookName = mEtBook.getText().toString();
                if(startType==MyConst.UPDATE_BOOK){
                    RecyclerViewAdapter.getDefaultRecyclerViewAdapter().getBookList().get(itemPosition).setName(bookName);
                }else{
                    RecyclerViewAdapter.getDefaultRecyclerViewAdapter().getBookList().add(new Book(bookName,R.drawable.book_1));
                }
                RecyclerViewAdapter.getDefaultRecyclerViewAdapter().notifyDataSetChanged();
                finish();
            }
        });
        mBtnCancel = findViewById(R.id.btn_cancel);
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}